var app = getApp();
var util = require('../../../utils/util.js');
var _DB = require('../../../utils/db.js');
var QR = require('../../../utils/wxqrcode.js');
var size;
const teacherID = app.globalData.teacherID;
const name = app.globalData.teacherName;

Page({


  data: {
    tabbar: ["进出二维码", "进出记录"],
    qrImg: "",
    tabIndex: 0,
    inoutList: [],
    appear:false,
  },


  infomation:function(){
    this.size = this.getQRCodeSize();
    this.setData({ teacherID: teacherID });
    var timestamp = Date.parse(new Date())/1000;
    var text=teacherID+"/"+timestamp;
    // this.setData({appear:true});
    // this.createQRCode(text, this.size)
    wx.cloud.callFunction({
      name:"get_student_openid",
      data:{
        studentID:this.data.teacherID
      }
    }).then(res=>{
      if(res.result.length==0){
        wx.cloud.callFunction({
          name:"addteacheropenid",
          data:{
            teacherID:this.data.teacherID,
          }
        }).then(res=>{
          console.log(res)
        })
      }
    })
    wx.requestSubscribeMessage({
      tmplIds: ['R5pgNdl5ew2oBKdnr3iPLAeWOYUBM9rfUyt9s4qtY1s'],
      success: res => {
        console.log(res)
        if (res.R5pgNdl5ew2oBKdnr3iPLAeWOYUBM9rfUyt9s4qtY1s == "accept"){
          this.setData({ appear: true });
          this.createQRCode(text, this.size)
        }else{
          wx.showToast({
            title: '不执行操作',
            icon:'none',
          })
        }
      },
      fail: err => {
        console.log('err', err)
      }
    })
  },

  //上面的tabbar
  tabbar: function (e) {
    const index = e.currentTarget.dataset.index;
    var _this = this
    var userid = this.data.teacherID
    this.setData({ tabIndex: index });
    if (index == 1) {
      wx.showToast({
        title: '努力获取中',
        icon: 'loading',
        mask: true
      })
      //获取数据库INorOUT信息
      _DB.getInOutByid(userid, function (res) {
        // console.log(res)
        var inoutList = _this.doList(res);
        _this.setData({ inoutList: inoutList })
      })
    }
  },
  onShow: function (e) {
    //获取时间戳
    this.time = new Date();
    //时间戳转换
    var date = util.formatTime(this.time);
    this.setData({ time: date })
  },

  //处理数组
  doList: function (list) {
    var timestamp;
    var identity;
    var inout;
    for (var i = 0; i < list.length; i++) {
      list[i].timestamp = this.doTime(list[i].timestamp);
      list[i].identity = this.doIdentity(list[i].identity);
      list[i].inorout = this.doInout(list[i].inorout);
    }
    return list.reverse();
  },
  //时间戳转换
  doTime: function (timestamp) {
    var date = new Date(timestamp * 1000);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate(); d = d < 10 ? ('0' + d) : d;
    var h = date.getHours(); h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds(); minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return m + '/' + d + ' ' + h + ':' + minute;
  },
  //身份转换
  doIdentity: function (identity) {
    if (identity == 3) {
      return "外来人员";
    }
  },
  //进出转换
  doInout: function (inout) {
    if (inout == 0) {
      return "正门进校";
    } else if(inout ==1){
      return "正门出校";
    } else if (inout == 2) {
      return "南门进校";
    } else if (inout == 3) {
      return "南门出校";
    }
  },

  //获取设备宽高
  getQRCodeSize: function () {
    var size = 0;
    try {
      var res = wx.getSystemInfoSync();
      var scale = res.windowWidth / 750;
      var width = 300 * scale;
      size = width;
    } catch (e) {
      // Do something when catch error
      console.log("获取设备信息失败" + e);
      size = 150;
    }
    return size;
  },

  //生成二维码
  createQRCode: function (text, size) {
    let that = this
    try {
      // console.log('QRcode: ', text, size)
      let _img = QR.createQrCodeImg(text, {
        size: parseInt(size)
      })
      that.setData({
        qrImg: _img
      })
    } catch (e) {
      console.log(e)
    }
  },
  onPullDownRefresh: function () {
    var text = this.data.teacherID + "/" + this.data.time;
    wx.showNavigationBarLoading()
    this.onShow()
    this.createQRCode(text, this.size)
    setTimeout(() => {
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    }, 1000);
  },
})